<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>《Nginx学习笔记》 - Lucklyの博客 - 在阅读中遇见自己</title><meta name="Description" content="早起的年轻人做的网站"><meta property="og:title" content="《Nginx学习笔记》" />
<meta property="og:description" content="
本文主要介绍nginx相关基础及简单使用
" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://luckly.work/nginx%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" />
<meta property="og:image" content="https://luckly.work/logo.png"/>
<meta property="article:published_time" content="2021-03-17T07:18:09+08:00" />
<meta property="article:modified_time" content="2021-03-17T07:18:09+08:00" />
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://luckly.work/logo.png"/>

<meta name="twitter:title" content="《Nginx学习笔记》"/>
<meta name="twitter:description" content="
本文主要介绍nginx相关基础及简单使用
"/>
<meta name="application-name" content="LoveIt">
<meta name="apple-mobile-web-app-title" content="LoveIt"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="https://luckly.work/nginx%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" /><link rel="prev" href="https://luckly.work/flutter%E5%95%86%E5%9F%8E%E9%A1%B9%E7%9B%AE/" /><link rel="next" href="https://luckly.work/linxu%E4%B8%8B%E5%AE%89%E8%A3%85mysql%E6%95%99%E7%A8%8B/" /><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/normalize.css@8.0.1/normalize.min.css"><link rel="stylesheet" href="/css/style.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.13.0/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.7.2/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "《Nginx学习笔记》",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/luckly.work\/nginx%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0\/"
        },"image": [{
                            "@type": "ImageObject",
                            "url": "https:\/\/luckly.work\/images\/Apple-Devices-Preview.png",
                            "width":  3200 ,
                            "height":  2048 
                        }],"genre": "posts","keywords": "Nginx","wordcount":  4972 ,
        "url": "https:\/\/luckly.work\/nginx%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0\/","datePublished": "2021-03-17T07:18:09+08:00","dateModified": "2021-03-17T07:18:09+08:00","license": "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.","publisher": {
            "@type": "Organization",
            "name": "xxxx","logo": {
                    "@type": "ImageObject",
                    "url": "https:\/\/luckly.work\/images\/avatar.png",
                    "width":  528 ,
                    "height":  560 
                }},"author": {
                "@type": "Person",
                "name": "早起的年轻人"
            },"description": ""
    }
    </script></head>
    <body header-desktop="fixed" header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="Lucklyの博客 - 在阅读中遇见自己"><span class="header-title-pre"><i class='far fa-kiss-wink-heart fa-fw'></i></span><span id="id-1" class="typeit"></span></a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 所有文章 </a><a class="menu-item" href="/categories/flutter/"> Flutter </a><a class="menu-item" href="/categories/read/"> 读书笔记 </a><a class="menu-item" href="/categories/go/"> Go </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><a class="menu-item" href="/about/"> 关于 </a><a class="menu-item" href="https://github.com/ITmxs" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw'></i> Github </a><a class="menu-item" href="/friend/"><i class='fas fa-user-friends'></i> 友链 </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-desktop">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="Lucklyの博客 - 在阅读中遇见自己"><span class="header-title-pre"><i class='far fa-kiss-wink-heart fa-fw'></i></span><span id="id-2" class="typeit"></span></a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-mobile">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </div>
                    <a href="javascript:void(0);" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">所有文章</a><a class="menu-item" href="/categories/flutter/" title="">Flutter</a><a class="menu-item" href="/categories/read/" title="">读书笔记</a><a class="menu-item" href="/categories/go/" title="">Go</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><a class="menu-item" href="/about/" title="">关于</a><a class="menu-item" href="https://github.com/ITmxs" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw'></i>Github</a><a class="menu-item" href="/friend/" title=""><i class='fas fa-user-friends'></i>友链</a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw"></i>
            </a></div>
    </div>
</header>
<div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div>
<main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animated flipInX">《Nginx学习笔记》</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel=" author" class="author"><i class="fas fa-user-circle fa-fw"></i>早起的年轻人</a></span>&nbsp;<span class="post-category">收录于 <a href="/categories/nginx/"><i class="far fa-folder fa-fw"></i>Nginx</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2021-03-17">2021-03-17</time>&nbsp;<i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 4972 字&nbsp;
                <i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 10 分钟&nbsp;<span id="/nginx%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" class="leancloud_visitors" data-flag-title="《Nginx学习笔记》">
                        <i class="far fa-eye fa-fw"></i>&nbsp;<span class=leancloud-visitors-count></span>&nbsp;次阅读
                    </span>&nbsp;</div>
        </div><div class="featured-image"><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://luckly007.oss-cn-beijing.aliyuncs.com/img/nginx.jpg"
        data-srcset="https://luckly007.oss-cn-beijing.aliyuncs.com/img/nginx.jpg, https://luckly007.oss-cn-beijing.aliyuncs.com/img/nginx.jpg 1.5x, https://luckly007.oss-cn-beijing.aliyuncs.com/img/nginx.jpg 2x"
        data-sizes="auto"
        alt="https://luckly007.oss-cn-beijing.aliyuncs.com/img/nginx.jpg"
        title="https://luckly007.oss-cn-beijing.aliyuncs.com/img/nginx.jpg" /></div><div class="details toc" id="toc-static"  kept="">
                <div class="details-summary toc-title">
                    <span>目录</span>
                    <span><i class="details-icon fas fa-angle-right"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li>
          <ul>
            <li><a href="#一nginx-简介">一、Nginx 简介</a>
              <ul>
                <li><a href="#1什么是nginx">1、什么是NGINX</a></li>
                <li><a href="#2反向代理"><strong>2、反向代理</strong></a></li>
                <li><a href="#3负载均衡">3、负载均衡</a></li>
                <li><a href="#4动静分离">4、动静分离</a></li>
              </ul>
            </li>
            <li><a href="#二nginx-安装">二、Nginx 安装</a>
              <ul>
                <li><a href="#例2-反向代理">例2 反向代理</a></li>
              </ul>
            </li>
            <li><a href="#四配置实例2-负载均衡">四、配置实例2 负载均衡</a>
              <ul>
                <li><a href="#负载分配策略">负载分配策略</a></li>
              </ul>
            </li>
            <li><a href="#五配置实例3-动静分离">五、配置实例3 动静分离</a></li>
            <li><a href="#六nginx配置高可用集群">六、Nginx配置高可用集群</a></li>
            <li><a href="#七nginx原理解析">七、Nginx原理解析</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><blockquote>
<p>本文主要介绍nginx相关基础及简单使用</p>
</blockquote>
<p>[TOC]</p>
<p><strong>内容介绍</strong>
<strong>1、nginx基本概念。</strong></p>
<blockquote>
<p>(1) nginx是什么，能做什么事情</p>
<p>(2) 反向代理。</p>
<p>(3) 负载均衡。</p>
<p>(4) 动静分离</p>
</blockquote>
<p><strong>2、nginx 安装、常用命令和配置文件</strong></p>
<blockquote>
<p>(1)在liunx系统中安装nginx.</p>
<p>(2) nginx常用命令。</p>
<p>(3) nginx 配置文件。</p>
</blockquote>
<p><strong>3、nginx 配置实例 1-反向代理</strong></p>
<p><strong>4、nginx 配置实例 2-负载均衡</strong></p>
<p><strong>5、nginx 配置实例 3-动静分离。</strong></p>
<p><strong>6、nginx配置高可用集群</strong></p>
<p><strong>7、nginx 原理。</strong></p>
<hr>
<h3 id="一nginx-简介">一、Nginx 简介</h3>
<h4 id="1什么是nginx">1、什么是NGINX</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">	Nginx (&#34;engine x&#34;)是一个高性能的HTTP和反向代理服务器，特点是占有内存少，并发能
力强，事实上nginx的并发能力确实在同类型的网页服务器中表现较好
    Nginx专为性能优化而开发，性能是其最重要的考量，实现上非常注重效率，能经受高负载
的考验，有报告表明能支持高达50000个并发连接数。
</code></pre></td></tr></table>
</div>
</div><h4 id="2反向代理"><strong>2、反向代理</strong></h4>
<p><strong>a. 正向代理</strong>
在客户端(浏览器)配置代理服务器，通过代理服务器进行互联网访问</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">{% asset_img image-20200606144302429.png  正向代理 %}
</code></pre></td></tr></table>
</div>
</div><p><strong>b. 反向代理</strong>
反向代理，其实客户端对代理是无感知的，因为客户端不需要任何配置就可以访问，我们只
需要将请求发送到反向代理服务器，由反向代理服务器去选择目标服务器获取数据后,在返
回给客户端，此时反向代理服务器和目标服务器对外就是一个服务器，暴露的是代理服务器
地址，隐藏了真实服务器IP地址。</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200606150932688.png"
        data-srcset="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200606150932688.png, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200606150932688.png 1.5x, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200606150932688.png 2x"
        data-sizes="auto"
        alt="https://nishigouzi.github.io/2020/06/07/Nginx笔记/image-20200606150932688.png"
        title="反向代理" /></p>
<h4 id="3负载均衡">3、负载均衡</h4>
<p>单个服务器解决不了，我们增加服务器的数量，然后将请求分发到各个服务器上,将原先
请求集中到单个服务器上的情况改为将请求分发到多个服务器上,将负载分发到不同的服
务器，也就是我们所说的负载均衡</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200606154956614.png"
        data-srcset="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200606154956614.png, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200606154956614.png 1.5x, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200606154956614.png 2x"
        data-sizes="auto"
        alt="https://nishigouzi.github.io/2020/06/07/Nginx笔记/image-20200606154956614.png"
        title="负载均衡" /></p>
<h4 id="4动静分离">4、动静分离</h4>
<p>为了加快网站的解析速度，可以把动态页面和静态页面由不同的服务器来解析，加快解析速
度。降低原来单个服务器的压力。</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200606155620164.png"
        data-srcset="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200606155620164.png, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200606155620164.png 1.5x, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200606155620164.png 2x"
        data-sizes="auto"
        alt="https://nishigouzi.github.io/2020/06/07/Nginx笔记/image-20200606155620164.png"
        title="动静分离" /></p>
<h3 id="二nginx-安装">二、Nginx 安装</h3>
<p>下面的操作是以Centos7为例</p>
<p><strong>1. 使用远程连接工具连接Centos7操作系统</strong>
<strong>2. 安装nginx相关依赖</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">gcc
pcre
openssl
zlib
</code></pre></td></tr></table>
</div>
</div><p>① 安装 nginx 需要先将官网下载的源码进行编译，编译依赖 gcc 环境，如果没有 gcc 环境，则需要安装：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ yum install gcc-c++
</code></pre></td></tr></table>
</div>
</div><p>② PCRE(Perl Compatible Regular Expressions) 是一个Perl库，包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式，所以需要在 linux 上安装 pcre 库，pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ yum install -y pcre pcre-devel
</code></pre></td></tr></table>
</div>
</div><p>③ zlib 库提供了很多种压缩和解压缩的方式， nginx 使用 zlib 对 http 包的内容进行 gzip ，所以需要在 Centos 上安装 zlib 库。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ yum install -y zlib zlib-devel
</code></pre></td></tr></table>
</div>
</div><p>④ OpenSSL 是一个强大的安全套接字层密码库，囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议，并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议，还支持 https（即在ssl协议上传输http），所以需要在 Centos 安装 OpenSSL 库。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ yum install -y openssl openssl-devel
</code></pre></td></tr></table>
</div>
</div><p><strong>3. 安装Nginx</strong></p>
<p>① 下载nginx，两种方式</p>
<blockquote>
<p>a. 直接下载<code>.tar.gz</code>安装包，地址：https://nginx.org/en/download.html</p>
<p>b. <strong>使用<code>wget</code>命令下载（推荐）</strong>。确保系统已经安装了wget，如果没有安装，执行 yum install wget 安装。</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ wget -c https://nginx.org/download/nginx-1.19.0.tar.gz
</code></pre></td></tr></table>
</div>
</div><p>② 依然是直接命令：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ tar -zxvf nginx-1.19.0.tar.gz
$ cd nginx-1.19.0
</code></pre></td></tr></table>
</div>
</div><p>③ 配置：</p>
<p>其实在 nginx-1.12.0 版本中你就不需要去配置相关东西，默认就可以了。当然，如果你要自己配置目录也是可以的。
1.使用默认配置</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ ./configure
</code></pre></td></tr></table>
</div>
</div><p>2.自定义配置（不推荐）</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ ./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>注：将临时文件目录指定为/var/temp/nginx，需要在/var下创建temp及nginx目录</p>
</blockquote>
<p>④ 编辑安装</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ make &amp;&amp; make install
</code></pre></td></tr></table>
</div>
</div><p>查看版本号(<code>使用nginx操作命令前提条件:必须进入nginx的目录/usr/local/nginx/sbin</code>.)</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ ./nginx -v
</code></pre></td></tr></table>
</div>
</div><p>查找安装路径：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ whereis nginx
</code></pre></td></tr></table>
</div>
</div><p>⑤ 启动，停止nginx</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ cd /usr/local/nginx/sbin/
$ ./nginx 
$ ./nginx -s stop
$ ./nginx -s quit
$ ./nginx -s reload
</code></pre></td></tr></table>
</div>
</div><p>查询nginx进程：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ ps aux|grep nginx
</code></pre></td></tr></table>
</div>
</div><p>启动成功后，在浏览器可以看到这样的页面：</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="C:/Users/ovo/Documents/2020-06-06-Nginx%e7%ac%94%e8%ae%b0/Nginx%e7%ac%94%e8%ae%b0/image-20200606173451847.png"
        data-srcset="C:/Users/ovo/Documents/2020-06-06-Nginx%e7%ac%94%e8%ae%b0/Nginx%e7%ac%94%e8%ae%b0/image-20200606173451847.png, C:/Users/ovo/Documents/2020-06-06-Nginx%e7%ac%94%e8%ae%b0/Nginx%e7%ac%94%e8%ae%b0/image-20200606173451847.png 1.5x, C:/Users/ovo/Documents/2020-06-06-Nginx%e7%ac%94%e8%ae%b0/Nginx%e7%ac%94%e8%ae%b0/image-20200606173451847.png 2x"
        data-sizes="auto"
        alt="C:/Users/ovo/Documents/2020-06-06-Nginx笔记/Nginx笔记/image-20200606173451847.png"
        title="image-20200606173451847" /></p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200606173451847.png"
        data-srcset="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200606173451847.png, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200606173451847.png 1.5x, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200606173451847.png 2x"
        data-sizes="auto"
        alt="https://nishigouzi.github.io/2020/06/07/Nginx笔记/image-20200606173451847.png"
        title="安装成功} &lt;strong&gt;可能遇到的问题： 远程访问服务器被拒绝&lt;/strong&gt; 可能是80端口被防火墙保护起来了，不让外界访问 查看开放的端口 &lt;figure class=highlight shell&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=gutter&gt;&lt;pre&gt;&lt;span class=line&gt;1&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=code&gt;&lt;pre&gt;&lt;span class=line&gt;&lt;span class=meta&gt;$&lt;/span&gt;&lt;span class=bash&gt; firewall-cmd &amp;ndash;list-all&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt; 打开固定端口 使用如下命令打开80端口（其他端口类似） &lt;figure class=highlight shell&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=gutter&gt;&lt;pre&gt;&lt;span class=line&gt;1&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;2&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=code&gt;&lt;pre&gt;&lt;span class=line&gt;&lt;span class=meta&gt;$&lt;/span&gt;&lt;span class=bash&gt; firewall-cmd &amp;ndash;add-service=http &amp;ndash;permanent&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;span class=meta&gt;$&lt;/span&gt;&lt;span class=bash&gt; firewall-cmd &amp;ndash;permanent &amp;ndash;add-port=80/tcp&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt; 注意这里需要声明tcp的方式，之后我们需要重新加载firewall的配置 &lt;figure class=highlight shell&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=gutter&gt;&lt;pre&gt;&lt;span class=line&gt;1&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=code&gt;&lt;pre&gt;&lt;span class=line&gt;&lt;span class=meta&gt;$&lt;/span&gt;&lt;span class=bash&gt; firewall-cmd &amp;ndash;reload&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt; ⑥ 开机自启动 即在&lt;code&gt;rc.local&lt;/code&gt;增加启动代码就可以了。 &lt;figure class=highlight shell&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=gutter&gt;&lt;pre&gt;&lt;span class=line&gt;1&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=code&gt;&lt;pre&gt;&lt;span class=line&gt;&lt;span class=meta&gt;$&lt;/span&gt;&lt;span class=bash&gt; vi /etc/rc.local&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt; 增加一行 &lt;code&gt;/usr/local/nginx/sbin/nginx&lt;/code&gt; 设置执行权限： &lt;figure class=highlight shell&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=gutter&gt;&lt;pre&gt;&lt;span class=line&gt;1&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=code&gt;&lt;pre&gt;&lt;span class=line&gt;&lt;span class=meta&gt;$&lt;/span&gt;&lt;span class=bash&gt; chmod 755 rc.local&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt; #### Nginx配置文件结构 nginx配置文件地址 &amp;gt; /usr/local/nginx/conf/nginx.conf 下面是默认config &lt;figure class=highlight plain&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=gutter&gt;&lt;pre&gt;&lt;span class=line&gt;1&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;2&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;3&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;4&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;5&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;6&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;7&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;8&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;9&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;10&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;11&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;12&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;13&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;14&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;15&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;16&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;17&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;18&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;19&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;20&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;21&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;22&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;23&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;24&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;25&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;26&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;27&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;28&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;29&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;30&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;31&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;32&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;33&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;34&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;35&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;36&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;37&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;38&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;39&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;40&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;41&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;42&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;43&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;44&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;45&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;46&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;47&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;48&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;49&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;50&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;51&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;52&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;53&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;54&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;55&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;56&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;57&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;58&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;59&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;60&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;61&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;62&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;63&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;64&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;65&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;66&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;67&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;68&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;69&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;70&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;71&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;72&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;73&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;74&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;75&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;76&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;77&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;78&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;79&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;80&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;81&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;82&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;83&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;84&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;85&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;86&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;87&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;88&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;89&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;90&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;91&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;92&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;93&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;94&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;95&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;96&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;97&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;98&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;99&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;100&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;101&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;102&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;103&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;104&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;105&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;106&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;107&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;108&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;109&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;110&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;111&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;112&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;113&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;114&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;115&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;116&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=code&gt;&lt;pre&gt;&lt;span class=line&gt;#user nobody;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;worker_processes 1;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;#error_log logs/error.log;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;#error_log logs/error.log notice;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;#error_log logs/error.log info;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;#pid logs/nginx.pid;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;events {&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; worker_connections 1024;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;}&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;http {&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; include mime.types;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; default_type application/octet-stream;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #log_format main &amp;lsquo;$remote_addr - $remote_user [$time_local] &amp;ldquo;$request&amp;rdquo; &amp;lsquo;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # &amp;lsquo;$status $body_bytes_sent &amp;ldquo;$http_referer&amp;rdquo; &amp;lsquo;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # &amp;lsquo;&amp;quot;$http_user_agent&amp;quot; &amp;ldquo;$http_x_forwarded_for&amp;rdquo;&#39;;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #access_log logs/access.log main;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; sendfile on;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #tcp_nopush on;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #keepalive_timeout 0;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; keepalive_timeout 65;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #gzip on;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; server {&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; listen 80;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; server_name localhost;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #charset koi8-r;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #access_log logs/host.access.log main;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; location / {&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; root html;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; index index.html index.htm;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; }&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #error_page 404 /404.html;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # redirect server error pages to the static page /50x.html&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; error_page 500 502 503 504 /50x.html;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; location = /50x.html {&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; root html;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; }&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # proxy the PHP scripts to Apache listening on 127.0.0.1:80&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #location ~ .php$ {&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # proxy_pass http://127.0.0.1;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #}&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #location ~ .php$ {&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # root html;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # fastcgi_pass 127.0.0.1:9000;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # fastcgi_index index.php;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # include fastcgi_params;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #}&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # deny access to .htaccess files, if Apache&amp;rsquo;s document root&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # concurs with nginx&amp;rsquo;s one&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #location ~ /.ht {&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # deny all;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #}&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; }&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # another virtual host using mix of IP-, name-, and port-based configuration&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #server {&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # listen 8000;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # listen somename:8080;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # server_name somename alias another.alias;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # location / {&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # root html;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # index index.html index.htm;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # }&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #}&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # HTTPS server&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #server {&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # listen 443 ssl;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # server_name localhost;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # ssl_certificate cert.pem;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # ssl_certificate_key cert.key;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # ssl_session_cache shared:SSL:1m;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # ssl_session_timeout 5m;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # ssl_ciphers HIGH:!aNULL:!MD5;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # ssl_prefer_server_ciphers on;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # location / {&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # root html;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # index index.html index.htm;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; # }&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #}&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;}&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt; nginx文件结构 &lt;figure class=highlight plain&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=gutter&gt;&lt;pre&gt;&lt;span class=line&gt;1&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;2&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;3&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;4&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;5&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;6&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;7&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;8&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;9&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;10&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;11&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;12&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;13&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;14&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;15&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;16&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;17&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;18&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;19&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;20&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;21&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;22&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;23&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;24&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;25&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;26&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;27&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=code&gt;&lt;pre&gt;&lt;span class=line&gt;&amp;hellip; #全局块&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;events { #events块&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; &amp;hellip;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;}&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;http #http块&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;{&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; &amp;hellip; #http全局块&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; server #server块&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; { &lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; &amp;hellip; #server全局块&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; location [PATTERN] #location块&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; {&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; &amp;hellip;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; }&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; location [PATTERN] &lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; {&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; &amp;hellip;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; }&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; }&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; server&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; {&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; &amp;hellip;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; }&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; &amp;hellip; #http全局块&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;}&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt; &amp;gt;1、全局块：配置影响nginx全局的指令。一般有运行nginx服务器的用户组，nginx进程pid存放路径，日志存放路径，配置文件引入，允许生成worker process数等。 &amp;gt; &amp;gt;2、events块：配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数，选取哪种事件驱动模型处理连接请求，是否允许同时接受多个网路连接，开启多个网络连接序列化等。 &amp;gt; &amp;gt;3、http块：可以嵌套多个server，配置代理，缓存，日志定义等绝大多数功能和第三方模块的配置。如文件引入，mime-type定义，日志自定义，是否使用sendfile传输文件，连接超时时间，单连接请求数等。 &amp;gt; &amp;gt;4、server块：配置虚拟主机的相关参数，一个http中可以有多个server。 &amp;gt; &amp;gt;5、location块：配置请求的路由，以及各种页面的处理情况。 配置文件示例 &lt;figure class=highlight plain&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=gutter&gt;&lt;pre&gt;&lt;span class=line&gt;1&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;2&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;3&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;4&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;5&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;6&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;7&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;8&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;9&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;10&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;11&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;12&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;13&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;14&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;15&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;16&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;17&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;18&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;19&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;20&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;21&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;22&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;23&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;24&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;25&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;26&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;27&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;28&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;29&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;30&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;31&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;32&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;33&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;34&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;35&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;36&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;37&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;38&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;39&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=code&gt;&lt;pre&gt;&lt;span class=line&gt;########### 每个指令必须有分号结束。#################&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;#user administrator administrators; #配置用户或者组，默认为nobody nobody。&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;#worker_processes 2; #允许生成的进程数，默认为1,这是Nginx服务器并发处理服务的关键配置, worker_processes值越大,可以支持的并发处理量也越多,但是会受到硬件、软件等设备的制约。&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;#pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;error_log log/error.log debug; #制定日志路径，级别。这个设置可以放入全局块，http块，server块，级别以此为：debug|info|notice|warn|error|crit|alert|emerg&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;events {&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; accept_mutex on; #设置网路连接序列化，防止惊群现象发生，默认为on&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; multi_accept on; #设置一个进程是否同时接受多个网络连接，默认为off&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #use epoll; #事件驱动模型，select|poll|kqueue|epoll|resig|/dev/poll|eventport&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; worker_connections 1024; #最大连接数，默认为512&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;}&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;http {&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; include mime.types; #文件扩展名与文件类型映射表&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; default_type application/octet-stream; #默认文件类型，默认为text/plain&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #access_log off; #取消服务日志 &lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; log_format myFormat &amp;lsquo;$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for&amp;rsquo;; #自定义格式&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; access_log log/access.log myFormat; #combined为日志格式的默认值&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; sendfile on; #允许sendfile方式传输文件，默认为off，可以在http块，server块，location块。&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值，默认为0，即不设上限。&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; keepalive_timeout 65; #连接超时时间，默认为75s，可以在http，server，location块。&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; upstream mysvr { &lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; server 127.0.0.1:7878;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; server 192.168.10.121:3333 backup; #热备&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; }&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; error_page 404 https://www.baidu.com; #错误页&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; server {&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; keepalive_requests 120; #单连接请求上限次数。&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; listen 4545; #监听端口&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; server_name 127.0.0.1; #监听地址 &lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; location ~*^.&#43;$ { #请求的url过滤，正则匹配，~为区分大小写，~*为不区分大小写。&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #root path; #根目录&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; #index vv.txt; #设置默认页&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; deny 127.0.0.1; #拒绝的ip&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; allow 172.18.5.54; #允许的ip &lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; } &lt;/span&gt;&lt;br&gt;&lt;span class=line&gt; }&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;}&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt; 上面是nginx的基本配置，需要注意的有以下几点： - 第一点 - $remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址； - $remote_user ：用来记录客户端用户名称； - $time_local ： 用来记录访问时间与时区； - $request ： 用来记录请求的url与http协议； - $status ： 用来记录请求状态；成功是200， - $body_bytes_s ent ：记录发送给客户端文件主体内容大小； - $http_referer ：用来记录从那个页面链接访问过来的； - $http_user_agent ：记录客户端浏览器的相关信息； - 惊群现象：一个网路连接到来，多个睡眠的进程被同事叫醒，但只有一个进程能获得链接，这样会影响系统性能。 - 每个指令必须有分号结束。 &lt;strong&gt;安装部分参考博客：https://www.cnblogs.com/lumama520/p/11208860.html&lt;/strong&gt; ### 三、配置实例1 反向代理 #### 例1 反向代理 &lt;strong&gt;1、实现效果&lt;/strong&gt; &amp;gt; 打开浏览器，在浏览器地址栏输入地址www.123.com, 跳转到liunx系统tomcat主页 &amp;gt; 面中。 &lt;strong&gt;2、准备工作&lt;/strong&gt; &amp;gt; 在linux系统安装jdk(使用了openjdk)，tomcat依赖jdk，安装目录：/usr/lib/jvm/java-1.8.0-openjdk-xx &amp;gt; &amp;gt; 在liunx系统安装tomcat,使用默认端口8080，安装目录：/usr/src/apache-tomcat-8.5.55 &lt;figure class=highlight shell&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=gutter&gt;&lt;pre&gt;&lt;span class=line&gt;1&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;2&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;3&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;4&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;5&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;6&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;7&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=code&gt;&lt;pre&gt;&lt;span class=line&gt;&lt;span class=meta&gt;#&lt;/span&gt;&lt;span class=bash&gt; 启动tomcat&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;span class=meta&gt;$&lt;/span&gt;&lt;span class=bash&gt; ./usr/src/apache-tomcat-8.5.55/bin/start.sh&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;span class=meta&gt;#&lt;/span&gt;&lt;span class=bash&gt; 查看tomcat 日志&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;span class=meta&gt;$&lt;/span&gt;&lt;span class=bash&gt; tail -f ./usr/src/apache-tomcat-8.5.55/logs/catalina.out&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;span class=meta&gt;#&lt;/span&gt;&lt;span class=bash&gt; 开放8080端口&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;span class=meta&gt;$&lt;/span&gt;&lt;span class=bash&gt; firewall-cmd &amp;ndash;permanent &amp;ndash;add-port=8080/tcp&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=line&gt;&lt;span class=meta&gt;$&lt;/span&gt;&lt;span class=bash&gt; firewall-cmd &amp;ndash;reload&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt; 准备工作结束 {% asset_img image-20200607094155776.png 访问过程" /></p>
<p><strong>实现工作</strong></p>
<p>修改nginx配置文件, <code>nginx.conf</code></p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="C:/Users/ovo/Documents/2020-06-06-Nginx%e7%ac%94%e8%ae%b0/Nginx%e7%ac%94%e8%ae%b0/image-20200607095215936.png"
        data-srcset="C:/Users/ovo/Documents/2020-06-06-Nginx%e7%ac%94%e8%ae%b0/Nginx%e7%ac%94%e8%ae%b0/image-20200607095215936.png, C:/Users/ovo/Documents/2020-06-06-Nginx%e7%ac%94%e8%ae%b0/Nginx%e7%ac%94%e8%ae%b0/image-20200607095215936.png 1.5x, C:/Users/ovo/Documents/2020-06-06-Nginx%e7%ac%94%e8%ae%b0/Nginx%e7%ac%94%e8%ae%b0/image-20200607095215936.png 2x"
        data-sizes="auto"
        alt="C:/Users/ovo/Documents/2020-06-06-Nginx笔记/Nginx笔记/image-20200607095215936.png"
        title="image-20200607095215936" /></p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607095215936.png"
        data-srcset="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607095215936.png, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607095215936.png 1.5x, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607095215936.png 2x"
        data-sizes="auto"
        alt="https://nishigouzi.github.io/2020/06/07/Nginx笔记/image-20200607095215936.png"
        title="改动部分" /></p>
<h4 id="例2-反向代理">例2 反向代理</h4>
<p><strong>1、实现效果</strong></p>
<blockquote>
<p>使用nginx反向代理，根据访问的路径跳转到不同端口的服务中。
nginx.监听端口为9001，。
访问http://127.0.0.1:9001/edu/ 直接跳转到127.0.0.1:8081
访问http://127.0.0.1:9001/vod/ 直接跳转到127.0.0.1:8082</p>
</blockquote>
<p><strong>2、准备工作</strong></p>
<blockquote>
<p>(1) 准备两个tomcat服务器，一个8080端口，一个8081端口</p>
<p>(2) 创建文件夹和测试页面。</p>
</blockquote>
<p><strong>3、具体nginx配置</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ vi /usr/local/nginx/conf/nginx.conf
</code></pre></td></tr></table>
</div>
</div><p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607121227387.png"
        data-srcset="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607121227387.png, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607121227387.png 1.5x, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607121227387.png 2x"
        data-sizes="auto"
        alt="https://nishigouzi.github.io/2020/06/07/Nginx笔记/image-20200607121227387.png"
        title="nginx配置" /></p>
<blockquote>
<p>(1) 找到nginx配置文件，进行反向代理配置。</p>
<p>(2) 开放对外访问的端口号9001</p>
<p>(3) 重启nginx服务器，使配置文件生效</p>
</blockquote>
<p><strong>4、最终测试</strong></p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607121539858.png"
        data-srcset="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607121539858.png, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607121539858.png 1.5x, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607121539858.png 2x"
        data-sizes="auto"
        alt="https://nishigouzi.github.io/2020/06/07/Nginx笔记/image-20200607121539858.png"
        title="edu请求示例" /></p>
<p><strong>5、补充<code>location</code>部分</strong></p>
<p>location指令说明。
该指令用于匹配URL。。
语法如下:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">location [ = | ~ | ~* | ^~] uri {

}
1、=: 用于不含正则表达式的uri前，要求请求字符串与uri严格匹配，如果匹配成功，
	就停止继续向下搜索并立即处理该请求
2、~: 用于表示uri包含正则表达式，并且区分大小写
3、~*: 用于表示uri包含正则表达式，并且不区分大小写
4、^~: 用于不含正则表达式的uri前，要求Nginx服务器找到标识uri和请求字
	符串匹配度最高的location后，立即使用此location处理请求，而不再使用location
	块中的正则uri和请求字符串做匹配
注意: 如果uri包含正则表达式，则必须要有~或者~*标识。
</code></pre></td></tr></table>
</div>
</div><h3 id="四配置实例2-负载均衡">四、配置实例2 负载均衡</h3>
<p><strong>1、实现效果</strong></p>
<blockquote>
<p>(1) 浏览器地址栏输入地址http://192.168.xxx.xxx/edu/index.html, 负载均衡效果，平均到8080
和8081端口中，</p>
</blockquote>
<p><strong>2、准备工作</strong></p>
<blockquote>
<p>(1) 准备两台tomcat服务器，一 台8080， 一台8081</p>
<p>(2) 在两台tomcat里面webapps目录中，创建名称是edu文件夹，在edu文件夹中创建
页面index.html,用于测试。</p>
</blockquote>
<p><strong>3、在nginx的配置文件中进行负载均衡的配置</strong></p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607124508114.png"
        data-srcset="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607124508114.png, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607124508114.png 1.5x, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607124508114.png 2x"
        data-sizes="auto"
        alt="https://nishigouzi.github.io/2020/06/07/Nginx笔记/image-20200607124508114.png"
        title="负载均衡配置" /></p>
<p><strong>4、效果</strong></p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607124828732.png"
        data-srcset="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607124828732.png, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607124828732.png 1.5x, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607124828732.png 2x"
        data-sizes="auto"
        alt="https://nishigouzi.github.io/2020/06/07/Nginx笔记/image-20200607124828732.png"
        title="负载均衡" /> <img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607124843736.png"
        data-srcset="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607124843736.png, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607124843736.png 1.5x, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607124843736.png 2x"
        data-sizes="auto"
        alt="https://nishigouzi.github.io/2020/06/07/Nginx笔记/image-20200607124843736.png"
        title="负载均衡" /></p>
<h4 id="负载分配策略">负载分配策略</h4>
<p>在linux下有Nginx、LVS、 Haproxy 等等服务可以提供负载均衡服务，而且Nginx提供了几种分配方式(策略):。</p>
<ul>
<li>
<p><strong>1、轮询(默认)</strong></p>
<p>每个请求按时间顺序逐一分配到不 同的后端服务器，如果后端服务器down掉，能自动剔除</p>
</li>
<li>
<p><strong>2、weight</strong>
weight代表权重默认为1,权重越高被分配的客户端越多。
指定轮询几率，weight和访问比率成正比，用于后端服务器性能不均的情况。例如: 。</p>
</li>
<li>
<p><strong>3、ip hash</strong></p>
<p>每个请求按访问ip的hash结果分配, 这样每个访客固定访问一个后端服务器,可以解诀session的问题。例如:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">upstream server pool{
  ip_ hash
  server 192.168.5.21:80
  server 192.168.5.22:80
}
</code></pre></td></tr></table>
</div>
</div></li>
<li>
<p><strong>4、fair (第三方)</strong>
按后端服务器的响应时间来分配请求，响应时间短的优先分配</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">upstream server_pool 
  server 192.168.5.21:80;
  server 192.168.5.22:80;
  fair;
}
</code></pre></td></tr></table>
</div>
</div></li>
</ul>
<h3 id="五配置实例3-动静分离">五、配置实例3 动静分离</h3>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607153752466.png"
        data-srcset="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607153752466.png, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607153752466.png 1.5x, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607153752466.png 2x"
        data-sizes="auto"
        alt="https://nishigouzi.github.io/2020/06/07/Nginx笔记/image-20200607153752466.png"
        title="动静分离" /></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">通过location指定不同的后缀名实现不同的请求转发。通过expires参数设置，可以使浏
览器缓存过期时间，减少与服务器之前的请求和流量。具体Expires定义: 是给一个资源
设定一个过期时间，也就是说无需去服务端验证，直接通过浏览器自身确认是否过期即可，
所以不会产生额外的流量。此种方法非常适合不经常变动的资源。(如果经常更新的文件，
不建议使用Expires来缓存)，如果设置3d, 表示在这3天之内访问这个URL, 发送一
个请求，比对服务器该文件最后更新时间没有变化，则不会从服务器抓取，返回状态码304,
如果有修改，则直接从服务器重新下载，返回状态码200。。
</code></pre></td></tr></table>
</div>
</div><p><strong>2、准备工作</strong></p>
<blockquote>
<p>(1) 在liunx系统中准备静态资源，用于进行访问</p>
<p>/data/image 图片文件夹</p>
<p>/data/www html文件夹</p>
</blockquote>
<p><strong>3、具体配置</strong></p>
<blockquote>
<p>(1) 在nginx配置文件中进行配置</p>
</blockquote>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607200057983.png"
        data-srcset="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607200057983.png, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607200057983.png 1.5x, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607200057983.png 2x"
        data-sizes="auto"
        alt="https://nishigouzi.github.io/2020/06/07/Nginx笔记/image-20200607200057983.png"
        title="动静分离" /></p>
<p>4、实际测试</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">http://192.168.1.112/www/index.html
http://192.168.1.112/image/1.jpg
</code></pre></td></tr></table>
</div>
</div><p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607200424689.png"
        data-srcset="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607200424689.png, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607200424689.png 1.5x, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607200424689.png 2x"
        data-sizes="auto"
        alt="https://nishigouzi.github.io/2020/06/07/Nginx笔记/image-20200607200424689.png"
        title="img" /></p>
<p>上图是因为<code>autoindex on</code>这个设置生效的</p>
<h3 id="六nginx配置高可用集群">六、Nginx配置高可用集群</h3>
<p><strong>1、什么是nginx高可用</strong></p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607205253505.png"
        data-srcset="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607205253505.png, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607205253505.png 1.5x, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607205253505.png 2x"
        data-sizes="auto"
        alt="https://nishigouzi.github.io/2020/06/07/Nginx笔记/image-20200607205253505.png"
        title="img" /></p>
<blockquote>
<p>(1) 需要两台nginx服务器。
(2) 需要keepalived
(3) 需要虚拟ip</p>
</blockquote>
<p><strong>2、配置高可用的准备工作</strong></p>
<blockquote>
<p>(1) 需要两台服务器192.168.17.129 和192.168.17.1314
(2) 在两台服务器安装nginx.
(3) 在两合服务器安装keepalived.</p>
</blockquote>
<p><strong>3、在两台服务器安装keepalived</strong>
使用yum命令进行安装</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ yum install keepalived
$ rpm -q -a keepalived    #查看是否已经安装上
</code></pre></td></tr></table>
</div>
</div><p>默认安装路径: /etc/keepalived</p>
<p>安装之后，在etc里面生成目录keepalived, 有配置文件keepalived.conf</p>
<p><strong>4、完成高可用配置(主从配置)</strong></p>
<p>（1）修改keepalived的配置文件<code>keepalived.conf</code>为：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">global_defs {
	notification_email {
	  acassen@firewall.loc
	  failover@firewall.loc
	  sysadmin@firewall.loc
	}
	notification_email_from Alexandre.Cassen@firewall.loc
	smtp_ server 192.168.17.129
	smtp_connect_timeout 30
	router_id LVS_DEVEL	# LVS_DEVEL这字段在/etc/hosts文件中看；通过它访问到主机
}

vrrp_script chk_http_ port {
	script &#34;/usr/local/src/nginx_check.sh&#34;
	interval 2   # (检测脚本执行的间隔)2s
	weight 2  #权重，如果这个脚本检测为真，服务器权重+2
}

vrrp_instance VI_1 {
	state BACKUP   # 备份服务器上将MASTER 改为BACKUP
	interface ens33 //网卡名称
	virtual_router_id 51 # 主、备机的virtual_router_id必须相同
	priority 100   #主、备机取不同的优先级，主机值较大，备份机值较小
	advert_int 1	#每隔1s发送一次心跳
	authentication {	# 校验方式， 类型是密码，密码1111
        auth type PASS
        auth pass 1111
    }
	virtual_ipaddress { # 虛拟ip
		192.168.17.50 // VRRP H虛拟ip地址
	}
}
</code></pre></td></tr></table>
</div>
</div><p>（2）在路径/usr/local/src/ 下新建检测脚本 nginx_check.sh</p>
<p>nginx_check.sh</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-bash" data-lang="bash"><span class="cp">#! /bin/bash
</span><span class="cp"></span><span class="nv">A</span><span class="o">=</span><span class="sb">`</span>ps -C nginx -no-header <span class="p">|</span> wc - 1<span class="sb">`</span>
<span class="k">if</span> <span class="o">[</span> <span class="nv">$A</span> -eq 0<span class="o">]</span><span class="p">;</span><span class="k">then</span>
	/usr/local/nginx/sbin/nginx
	sleep <span class="m">2</span>
	<span class="k">if</span> <span class="o">[</span><span class="sb">`</span>ps -C nginx --no-header<span class="p">|</span> wc -1<span class="sb">`</span> -eq <span class="m">0</span> <span class="o">]</span><span class="p">;</span><span class="k">then</span>
		killall keepalived
	<span class="k">fi</span>
<span class="k">fi</span>
</code></pre></td></tr></table>
</div>
</div><p>(3) 把两台服务器上nginx和keepalived启动</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ systemctl start keepalived.service		#keepalived启动
$ ps -ef I grep keepalived		#查看keepalived是否启动
</code></pre></td></tr></table>
</div>
</div><p><strong>5、最终测试</strong></p>
<p>(1) 在浏览器地址栏输入虚拟ip地址192.168.17.50</p>
<p>(2) 把主服务器(192.168.17.129) nginx和keealived停止，再输入192.168.17.50.</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">$ systemctl stop keepalived.service  #keepalived停止
</code></pre></td></tr></table>
</div>
</div><h3 id="七nginx原理解析">七、Nginx原理解析</h3>
<p><strong>1、master和worker</strong></p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607222133869.png"
        data-srcset="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607222133869.png, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607222133869.png 1.5x, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607222133869.png 2x"
        data-sizes="auto"
        alt="https://nishigouzi.github.io/2020/06/07/Nginx笔记/image-20200607222133869.png"
        title="img" /></p>
<p><strong>2、worker如何进行工作的</strong></p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607222501486.png"
        data-srcset="https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607222501486.png, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607222501486.png 1.5x, https://nishigouzi.github.io/2020/06/07/Nginx%e7%ac%94%e8%ae%b0/image-20200607222501486.png 2x"
        data-sizes="auto"
        alt="https://nishigouzi.github.io/2020/06/07/Nginx笔记/image-20200607222501486.png"
        title="img" /></p>
<p><strong>3、一个master和多个woker的好处</strong>
(1) 可以使用nginx -s reload热部署。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">首先，对于每个worker进程来说，独立的进程，不需要加锁，所以省掉了锁带来的开销，
同时在编程以及问题查找时，也会方便很多。其次,采用独立的进程，可以让互相之间不会
影响，一个进程退出后，其它进程还在工作，服务不会中断，master进程则很快启动新的
worker进程。当然，worker进程的异常退出，肯定是程序有bug了，异常退出，会导致当
前worker.上的所有请求失败，不过不会影响到所有请求，所以降低了风险。
</code></pre></td></tr></table>
</div>
</div><p><strong>4、设置多少个woker合适</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">Nginx同redis类似都采用了io多路复用机制，每个worker都是一个独立的进程， 但每个进
程里只有一个主线程，通过异步非阻塞的方式来处理请求，即使是 千上万个请求也不在话
下。每个worker的线程可以把一个cpu的性能发挥到极致。所以worker数和服务器的cpu
数相等是最为适宜的。设少了会浪费cpu,设多了会造成cpu频繁切换上下文带来的损耗。
# 设置worker数量
worker.processes 4 

# work绑定cpu(4work绑定4cpu)
worker_cpu_affinity 0001 0010 0100 1000

# work绑定cpu (4work绑定8cpu中的4个)
worker_cpu_affinity 0000001 00000010 00000100 00001000
</code></pre></td></tr></table>
</div>
</div><p>**5、连接数worker_ connection**</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">这个值是表示每个worker进程所能建立连接的最大值，所以，一个nginx 能建立的最大连接数，应该是worker.connections * worker processes。当然，这里说的是最大连接数，对于HTTP 请求本地资源来说，能够支持的最大并发数量是worker.connections * worker processes,如果是支持http1.1的浏览器每次访问要占两个连接，所以普通的静态访问最大并发数是: worker.connections * worker.processes / 2, 而如果是HTTP作为反向代理来说，最大并发数量应该是worker.connections * worker_proceses/4. 因为作为反向代理服务器，每个并发会建立与客户端的连接和与后端服务的连接，会占用两个连接.
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>第一个: 发送请求，占用了woker的几个连接数?
答案: 2或者4个。</p>
<p>第二个: nginx有一个master,有四个woker,每个woker支持最大的连接数1024,支持的最大并发数是多少?
答案：普通的静态访问最大并发数是: worker connections * worker processes /2，
而如果是HTTP作为反向代理来说，最大并发数量应该是worker connections * worker processes/4</p>
</blockquote></div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>更新于 2021-03-17</span>
            </div>
            <div class="post-info-license"></div>
        </div>
        <div class="post-info-line">
            <div class="post-info-md"></div>
            <div class="post-info-share">
                <span><a href="javascript:void(0);" title="分享到 Twitter" data-sharer="twitter" data-url="https://luckly.work/nginx%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" data-title="《Nginx学习笔记》" data-hashtags="Nginx"><i class="fab fa-twitter fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Facebook" data-sharer="facebook" data-url="https://luckly.work/nginx%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" data-hashtag="Nginx"><i class="fab fa-facebook-square fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Hacker News" data-sharer="hackernews" data-url="https://luckly.work/nginx%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" data-title="《Nginx学习笔记》"><i class="fab fa-hacker-news fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Line" data-sharer="line" data-url="https://luckly.work/nginx%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" data-title="《Nginx学习笔记》"><i data-svg-src="https://cdn.jsdelivr.net/npm/simple-icons@2.14.0/icons/line.svg"></i></a><a href="javascript:void(0);" title="分享到 微博" data-sharer="weibo" data-url="https://luckly.work/nginx%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" data-title="《Nginx学习笔记》" data-image="https://luckly007.oss-cn-beijing.aliyuncs.com/img/nginx.jpg"><i class="fab fa-weibo fa-fw"></i></a></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw"></i>&nbsp;<a href="/tags/nginx/">Nginx</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="/">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="/flutter%E5%95%86%E5%9F%8E%E9%A1%B9%E7%9B%AE/" class="prev" rel="prev" title="《Flutter商城项目01》"><i class="fas fa-angle-left fa-fw"></i>《Flutter商城项目01》</a>
            <a href="/linxu%E4%B8%8B%E5%AE%89%E8%A3%85mysql%E6%95%99%E7%A8%8B/" class="next" rel="next" title="《Linxu下安装Mysql教程》">《Linxu下安装Mysql教程》<i class="fas fa-angle-right fa-fw"></i></a></div>
</div>
<div id="comments"><div id="valine" class="comment"></div><noscript>
                Please enable JavaScript to view the comments powered by <a href="https://valine.js.org/">Valine</a>.
            </noscript></div></article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.79.1">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.10"><i class="far fa-kiss-wink-heart fa-fw"></i> LoveIt</a>
                </div><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2021</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">早起的年轻人</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><link rel="stylesheet" href="/lib/valine/valine.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/cookieconsent@3.1.1/build/cookieconsent.min.css"><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/smooth-scroll@16.1.3/dist/smooth-scroll.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/autocomplete.js@0.37.1/dist/autocomplete.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/algoliasearch@4.2.0/dist/algoliasearch-lite.umd.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lazysizes@5.2.2/lazysizes.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sharer.js@0.4.0/sharer.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/typeit@7.0.4/dist/typeit.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/cookieconsent@3.1.1/build/cookieconsent.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":10},"comment":{"valine":{"appId":"LqsxahhaGjA96LY5Ldj1mbgq-MdYXbMMI","appKey":"Hdb0imPTOlWHWe8MY3bD8tln","avatar":"robohash","el":"#valine","emojiCDN":"https://cdn.jsdelivr.net/npm/emoji-datasource-google@5.0.1/img/google/64/","emojiMaps":{"100":"1f4af.png","alien":"1f47d.png","anger":"1f4a2.png","angry":"1f620.png","anguished":"1f627.png","astonished":"1f632.png","black_heart":"1f5a4.png","blue_heart":"1f499.png","blush":"1f60a.png","bomb":"1f4a3.png","boom":"1f4a5.png","broken_heart":"1f494.png","brown_heart":"1f90e.png","clown_face":"1f921.png","cold_face":"1f976.png","cold_sweat":"1f630.png","confounded":"1f616.png","confused":"1f615.png","cry":"1f622.png","crying_cat_face":"1f63f.png","cupid":"1f498.png","dash":"1f4a8.png","disappointed":"1f61e.png","disappointed_relieved":"1f625.png","dizzy":"1f4ab.png","dizzy_face":"1f635.png","drooling_face":"1f924.png","exploding_head":"1f92f.png","expressionless":"1f611.png","face_vomiting":"1f92e.png","face_with_cowboy_hat":"1f920.png","face_with_hand_over_mouth":"1f92d.png","face_with_head_bandage":"1f915.png","face_with_monocle":"1f9d0.png","face_with_raised_eyebrow":"1f928.png","face_with_rolling_eyes":"1f644.png","face_with_symbols_on_mouth":"1f92c.png","face_with_thermometer":"1f912.png","fearful":"1f628.png","flushed":"1f633.png","frowning":"1f626.png","ghost":"1f47b.png","gift_heart":"1f49d.png","green_heart":"1f49a.png","grimacing":"1f62c.png","grin":"1f601.png","grinning":"1f600.png","hankey":"1f4a9.png","hear_no_evil":"1f649.png","heart":"2764-fe0f.png","heart_decoration":"1f49f.png","heart_eyes":"1f60d.png","heart_eyes_cat":"1f63b.png","heartbeat":"1f493.png","heartpulse":"1f497.png","heavy_heart_exclamation_mark_ornament":"2763-fe0f.png","hole":"1f573-fe0f.png","hot_face":"1f975.png","hugging_face":"1f917.png","hushed":"1f62f.png","imp":"1f47f.png","innocent":"1f607.png","japanese_goblin":"1f47a.png","japanese_ogre":"1f479.png","joy":"1f602.png","joy_cat":"1f639.png","kiss":"1f48b.png","kissing":"1f617.png","kissing_cat":"1f63d.png","kissing_closed_eyes":"1f61a.png","kissing_heart":"1f618.png","kissing_smiling_eyes":"1f619.png","laughing":"1f606.png","left_speech_bubble":"1f5e8-fe0f.png","love_letter":"1f48c.png","lying_face":"1f925.png","mask":"1f637.png","money_mouth_face":"1f911.png","nauseated_face":"1f922.png","nerd_face":"1f913.png","neutral_face":"1f610.png","no_mouth":"1f636.png","open_mouth":"1f62e.png","orange_heart":"1f9e1.png","partying_face":"1f973.png","pensive":"1f614.png","persevere":"1f623.png","pleading_face":"1f97a.png","pouting_cat":"1f63e.png","purple_heart":"1f49c.png","rage":"1f621.png","relaxed":"263a-fe0f.png","relieved":"1f60c.png","revolving_hearts":"1f49e.png","right_anger_bubble":"1f5ef-fe0f.png","robot_face":"1f916.png","rolling_on_the_floor_laughing":"1f923.png","scream":"1f631.png","scream_cat":"1f640.png","see_no_evil":"1f648.png","shushing_face":"1f92b.png","skull":"1f480.png","skull_and_crossbones":"2620-fe0f.png","sleeping":"1f634.png","sleepy":"1f62a.png","slightly_frowning_face":"1f641.png","slightly_smiling_face":"1f642.png","smile":"1f604.png","smile_cat":"1f638.png","smiley":"1f603.png","smiley_cat":"1f63a.png","smiling_face_with_3_hearts":"1f970.png","smiling_imp":"1f608.png","smirk":"1f60f.png","smirk_cat":"1f63c.png","sneezing_face":"1f927.png","sob":"1f62d.png","space_invader":"1f47e.png","sparkling_heart":"1f496.png","speak_no_evil":"1f64a.png","speech_balloon":"1f4ac.png","star-struck":"1f929.png","stuck_out_tongue":"1f61b.png","stuck_out_tongue_closed_eyes":"1f61d.png","stuck_out_tongue_winking_eye":"1f61c.png","sunglasses":"1f60e.png","sweat":"1f613.png","sweat_drops":"1f4a6.png","sweat_smile":"1f605.png","thinking_face":"1f914.png","thought_balloon":"1f4ad.png","tired_face":"1f62b.png","triumph":"1f624.png","two_hearts":"1f495.png","unamused":"1f612.png","upside_down_face":"1f643.png","weary":"1f629.png","white_frowning_face":"2639-fe0f.png","white_heart":"1f90d.png","wink":"1f609.png","woozy_face":"1f974.png","worried":"1f61f.png","yawning_face":"1f971.png","yellow_heart":"1f49b.png","yum":"1f60b.png","zany_face":"1f92a.png","zipper_mouth_face":"1f910.png","zzz":"1f4a4.png"},"enableQQ":true,"highlight":true,"lang":true,"meta":true,"pageSize":true,"placeholder":"说点什么吧...","recordIP":true,"visitor":true}},"cookieconsent":{"content":{"dismiss":"同意","link":"了解更多","message":"本网站使用 Cookies 来改善您的浏览体验."},"enable":true,"palette":{"button":{"background":"#f0f0f0"},"popup":{"background":"#1aa3ff"}},"theme":"edgeless"},"data":{"id-1":"早起的年轻人","id-2":"早起的年轻人"},"search":{"algoliaAppID":"PASDMWALPK","algoliaIndex":"index.zh-cn","algoliaSearchKey":"b42948e51daaa93df92381c8e2ac0f93","highlightTag":"em","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":50,"type":"algolia"},"typeit":{"cursorChar":"|","cursorSpeed":1000,"data":{"id-1":["id-1"],"id-2":["id-2"]},"duration":-1,"speed":100}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
